home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / fortune-.tar / fortune- / fortune / util / randstr.c < prev    next >
C/C++ Source or Header  |  1995-10-01  |  8KB  |  227 lines

  1. /*
  2.  * The following code has been derived chiefly from the BSD distributions
  3.  * of the utility program unstr and the random quote displayer fortune.
  4.  * The utility produced by this code shares characteristics of both
  5.  * (it might be regarded as a minimalist implementation of fortune, for
  6.  * large values of 'minimal'), and is here offered so as to have the
  7.  * minimal necessary tools for rabbiting a strfile routine into some other,
  8.  * more significant program, all in one place.  A programmer who cares and
  9.  * has the proper training could probably clean this up significantly; it's
  10.  * all stolen code (first rule of programming: steal) hacked together to
  11.  * fit.  Or, to paraphrase the old saw about how the British built ships,
  12.  * it's coded by the mile and cut off to order.  In that analogy, this
  13.  * program's about an inch--and separated with an axe.
  14.  * 
  15.  * Axe murderess programming.  Wotta concept!
  16.  *
  17.  * Use at your own peril, especially as a pattern (kludge, kludge!). This
  18.  * program, at least, shouldn't have any real chance of corrupting data,
  19.  * though; it opens files ro and dumps to the screen.  If you redirect
  20.  * output, you definitely do so at your own peril (I lost six hours of
  21.  * editing on a fortune file that way, by redirecting the output of unstr
  22.  * before it had an outputfile option, trying to skip over the mv x.sorted
  23.  * x step.  Axe murderess redirection, in that case).
  24.  * 
  25.  * Blame Amy A. Lewis.  September, 1995.  alewis@email.unc.edu
  26.  */
  27.  
  28. /*-
  29.  * Copyright (c) 1991, 1993
  30.  *    The Regents of the University of California.  All rights reserved.
  31.  *
  32.  * This code is derived from software contributed to Berkeley by
  33.  * Ken Arnold.
  34.  *
  35.  * Redistribution and use in source and binary forms, with or without
  36.  * modification, are permitted provided that the following conditions
  37.  * are met:
  38.  * 1. Redistributions of source code must retain the above copyright
  39.  *    notice, this list of conditions and the following disclaimer.
  40.  * 2. Redistributions in binary form must reproduce the above copyright
  41.  *    notice, this list of conditions and the following disclaimer in the
  42.  *    documentation and/or other materials provided with the distribution.
  43.  * 3. All advertising materials mentioning features or use of this software
  44.  *    must display the following acknowledgement:
  45.  *    This product includes software developed by the University of
  46.  *    California, Berkeley and its contributors.
  47.  * 4. Neither the name of the University nor the names of its contributors
  48.  *    may be used to endorse or promote products derived from this software
  49.  *    without specific prior written permission.
  50.  *
  51.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  52.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  53.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  54.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  55.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  56.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  57.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  58.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  59.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  60.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  61.  * SUCH DAMAGE.
  62.  */
  63.  
  64. /* randstr repeats the minimum functionality of the fortune program.  It
  65.  * finds the fortune text or data file specified on the command line --
  66.  * one or the other, not both -- generates a random number, and displays
  67.  * the text string indexed.  No provision is made for any other command
  68.  * line switches.  At all.
  69.  * 
  70.  * Usage:
  71.  * 
  72.  * randstr filename[.ext]
  73.  * 
  74.  * Example: run sed or Perl over your /etc/passwd, and kick out a 
  75.  * strfile-format file containing lognames on the first line and full
  76.  * names on the second.  Write a script called 'lottery' which is
  77.  * called once a month from crontab; it in turn calls randstr lusers,
  78.  * and the winning luser gets a prize notification sent by email from
  79.  * the lottery script.  Living up to promises is optional.
  80.  * 
  81.  * Note: if you're a sysadmin who regularly reads _Mein Kampf_ for the
  82.  * deep truths buried in it, and believe in Truth, Justice, and the
  83.  * American Family, you could use this to replace fortune, by pointing
  84.  * it at a small, Family Values database.  The great advantage to this,
  85.  * in my opinion, is that it wouldn't take up any disk space at all.
  86.  * Who're you gonna quote?  Dan Quayle?
  87.  */
  88.  
  89. #include    <netinet/in.h>
  90. #include    <sys/param.h>
  91. #include    "strfile.h"
  92. #include    <stdio.h>
  93. #include    <stdlib.h>
  94. #include    <ctype.h>
  95. #include    <string.h>
  96. #include    <unistd.h>
  97. #include    <time.h>
  98. #ifndef MAXPATHLEN
  99. #define    MAXPATHLEN    1024
  100. #endif /* MAXPATHLEN */
  101.  
  102. char *Infile,            /* name of input file */
  103.   Datafile[MAXPATHLEN],        /* name of data file */
  104.   Delimch;            /* delimiter character */
  105.  
  106. FILE *Inf, *Dataf, *Outf;
  107.  
  108. off_t pos, Seekpts[2];        /* seek pointers to fortunes */
  109.  
  110.  
  111. void getargs(int ac, char *av[])
  112. {
  113.     extern int optind;
  114.     char *extc;
  115.  
  116.     av += optind + 1;
  117.  
  118.     if (*av)
  119.     {
  120.     Infile = *av;
  121. /* Hmm.  Don't output anything if we can help it.
  122.  * fprintf(stderr, "Input file: %s\n",Infile); */
  123.     if (!strrchr(Infile, '.'))
  124.     {
  125.         strcpy(Datafile, Infile);
  126.         strcat(Datafile, ".dat");
  127.     }
  128.     else
  129.     {
  130.         strcpy(Datafile, Infile);
  131.         extc = strrchr(Infile, '.');
  132.         *extc = '\0';
  133.     }
  134.     }
  135.     else
  136. /*    {
  137.  * Don't write out errors here, either; trust in exit codes and sh
  138.  * fprintf(stderr, "No input file name\n");
  139.  * fprintf(stderr, "Usage:\n\tunstr [-c C] datafile[.ext] [outputfile]\n");
  140.  */ exit(1);
  141. /*    } */
  142. }
  143.  
  144. /*
  145.  * get_pos:
  146.  *      Get the position from the pos file, if there is one.  If not,
  147.  *      return a random number.
  148.  */
  149. void get_pos(STRFILE * fp)
  150. {
  151.     pos = random() % fp->str_numstr;
  152.     if (++(pos) >= fp->str_numstr)
  153.     pos -= fp->str_numstr;
  154. }
  155.  
  156. /*
  157.  * get_fort:
  158.  *      Get the fortune data file's seek pointer for the next fortune.
  159.  */
  160. void get_fort(STRFILE fp)
  161. {
  162.     register int choice;
  163.  
  164.     choice = random() % fp.str_numstr;
  165.  
  166.     get_pos(&fp);
  167.     fseek(Dataf, (long) (sizeof fp + pos * sizeof Seekpts[0]), 0);
  168.     fread(Seekpts, sizeof Seekpts, 1, Dataf);
  169.     Seekpts[0] = ntohl(Seekpts[0]);
  170.     Seekpts[1] = ntohl(Seekpts[1]);
  171. }
  172.  
  173. void display(FILE * fp, STRFILE table)
  174. {
  175.     register char *p, ch;
  176.     unsigned char line[BUFSIZ];
  177.     int i;
  178.  
  179.     fseek(fp, (long) Seekpts[0], 0);
  180.     for (i = 0; fgets(line, sizeof line, fp) != NULL &&
  181.      !STR_ENDSTRING(line, table); i++)
  182.     {
  183.     if (table.str_flags & STR_ROTATED)
  184.         for (p = line; (ch = *p); ++p)
  185.         if (isupper(ch))
  186.             *p = 'A' + (ch - 'A' + 13) % 26;
  187.         else if (islower(ch))
  188.             *p = 'a' + (ch - 'a' + 13) % 26;
  189.     fputs(line, stdout);
  190.     }
  191.     fflush(stdout);
  192. }
  193.  
  194. int main(int ac, char **av)
  195. {
  196.     static STRFILE tbl;        /* description table */
  197.  
  198.     getargs(ac, av);
  199.     if ((Inf = fopen(Infile, "r")) == NULL)
  200.     {
  201.     perror(Infile);
  202.     exit(1);
  203.     }
  204.     if ((Dataf = fopen(Datafile, "r")) == NULL)
  205.     {
  206.     perror(Datafile);
  207.     exit(1);
  208.     }
  209.     fread((char *) &tbl, sizeof tbl, 1, Dataf);
  210.     tbl.str_version = ntohl(tbl.str_version);
  211.     tbl.str_numstr = ntohl(tbl.str_numstr);
  212.     tbl.str_longlen = ntohl(tbl.str_longlen);
  213.     tbl.str_shortlen = ntohl(tbl.str_shortlen);
  214.     tbl.str_flags = ntohl(tbl.str_flags);
  215.  
  216.     srandom((int) (time((time_t *) NULL) + getpid()));
  217.     get_fort(tbl);
  218.     display(Inf, tbl);
  219.  
  220.     exit(0);
  221.  
  222.     fclose(Inf);
  223.     fclose(Dataf);
  224.     fclose(Outf);
  225.     exit(0);
  226. }
  227.